﻿using System.ComponentModel;

namespace Microsoft.AspNetCore.Http
{
    /// <summary>
    /// 令牌错误类型常量定义
    /// </summary>
    public static class TokenErrorType
    {
        /// <summary>
        /// 无错误
        /// </summary>
        public const string None = "none";

        /// <summary>
        /// 令牌已过期
        /// </summary>
        public const string Expired = "expired";

        /// <summary>
        /// 无效的令牌
        /// </summary>
        public const string Invalid = "invalid";

        /// <summary>
        /// 令牌签名无效
        /// </summary>
        public const string InvalidSignature = "invalid_signature";

        /// <summary>
        /// 令牌发行者无效
        /// </summary>
        public const string InvalidIssuer = "invalid_issuer";

        /// <summary>
        /// 令牌已被列入黑名单
        /// </summary>
        public const string Blacklisted = "blacklisted";
    }

    /// <summary>
    /// Token错误类型扩展方法
    /// </summary>
    public static class TokenErrorTypeExtensions
    {
        /// <summary>
        /// 获取错误类型的描述信息
        /// </summary>
        /// <param name="errorType">错误类型</param>
        /// <returns>错误描述</returns>
        public static string GetDescription(this string errorType)
        {
            return errorType switch
            {
                TokenErrorType.None => "无错误",
                TokenErrorType.Expired => "令牌已过期，请重新登录",
                TokenErrorType.Invalid => "无效的访问令牌",
                TokenErrorType.InvalidSignature => "令牌签名无效",
                TokenErrorType.InvalidIssuer => "令牌发行者无效",
                TokenErrorType.Blacklisted => "令牌已被吊销",
                _ => "未知的令牌错误"
            };
        }

        /// <summary>
        /// 转换为标准的 OAuth 2.0 错误代码
        /// </summary>
        /// <param name="errorType">错误类型</param>
        /// <returns>OAuth 2.0 错误代码</returns>
        public static string ToOAuthErrorCode(this string errorType)
        {
            return errorType switch
            {
                TokenErrorType.Expired => "token_expired",
                TokenErrorType.Invalid or
                TokenErrorType.InvalidSignature or
                TokenErrorType.InvalidIssuer or
                TokenErrorType.Blacklisted => "invalid_token",
                _ => string.Empty
            };
        }
    }
}